//
//  WlwSearchController.m
//  busRoute
//
//  Created by 物恋网 on 16/11/16.
//  Copyright © 2016年 wlw. All rights reserved.
//

#import "WlwSearchController.h"
#import "KxMenu.h"
#import "CustomBtn.h"
@interface WlwSearchController ()<UISearchBarDelegate>
@property (nonatomic, strong) UISearchBar *searchBarView;
@property (nonatomic, strong) UILabel *placeHolderLbl;
@property (nonatomic, strong) CustomBtn *selectBtn;
@property (nonatomic, strong) NSNumber *search;
@end

@implementation WlwSearchController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addSearchBarView];
}


- (void)addSearchBarView {
    UIView *searchBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 75.f)];
    
    searchBgView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:searchBgView];
    
    UISearchBar *searchBarView = [[UISearchBar alloc] initWithFrame:CGRectMake(8.f, 27.f,SCREEN_WIDTH-58.f, 40.f)];
    
    //光标颜色
    searchBarView.tintColor = [UIColor grayColor];
    
    [searchBarView setBackgroundImage:[UIImage imageNamed:@"clear_bg"]];
    
    //背景图片
  //  [searchBarView setSearchFieldBackgroundImage:[UIImage imageNamed:@"searchFiledBg"] forState:UIControlStateNormal];
    searchBarView.barTintColor =VIEWBACKCOLOR;
    UIImageView *barImageView = [[[searchBarView.subviews firstObject] subviews] firstObject];
    barImageView.layer.borderColor = [UIColor colorWithRed:248/255.f green:248/255.f blue:248/255.f alpha:1.0].CGColor;
    barImageView.layer.borderWidth = 1;
      UIView *searchTextField = searchTextField = [[[searchBarView.subviews firstObject] subviews] lastObject];
    searchTextField.backgroundColor = VIEWBACKCOLOR;
    
    //输入光标左边距
    [searchBarView setSearchTextPositionAdjustment: UIOffsetMake(70, 0)];
    
    searchBarView.delegate = self;
    
    self.searchBarView = searchBarView;
    
    //隐藏放大镜图标
    UITextField *txfSearchField = [searchBarView valueForKey:@"_searchField"];
    
    [txfSearchField setLeftViewMode:UITextFieldViewModeNever];
    
    //自定义取消按钮
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-57.f, CGRectGetMinY(searchBarView.frame), 57.f, 44.f)];
    
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:TEXT_FONT+1];
    
    [cancelBtn setTitleColor:RGBACOLOR(0x33, 0x33, 0x33, 1.0) forState:UIControlStateNormal];
    
    [cancelBtn addTarget:self action:@selector(cancelSearch) forControlEvents:UIControlEventTouchUpInside];
    
    //自定义placeholder text
    UILabel *label = [UILabel new];
    
   // label.frame = CGRectMake(65, 8, 150, 28);
    
    label.text = @"请输入您要查询的位置";
    
    label.font = [UIFont systemFontOfSize:TEXT_FONT];
    
    label.textColor = RGBACOLOR(0x99, 0x99, 0x99, 1.0);
    
    [searchBarView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(searchBarView).offset(70);
        make.centerY.equalTo(searchBarView);
        make.width.equalTo(@150);
        make.height.equalTo(@28);
    }];
    
    self.placeHolderLbl = label;
    
    [searchBgView addSubview:searchBarView];
    
    [searchBgView addSubview:cancelBtn];
    _selectBtn = [[CustomBtn alloc] initWithFrame:CGRectZero];
    _selectBtn.titleLab.text = @"位置";
    _selectBtn.userInteractionEnabled = YES;
    [searchBarView addSubview:_selectBtn];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showMenu:)];
    [_selectBtn addGestureRecognizer:tap];
    
    [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(searchBarView).offset(0);
        make.centerY.equalTo(searchBarView);
        make.width.equalTo(@60);
        make.height.equalTo(@35);
    }];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    for(UIView *subView in self.searchBarView.subviews.firstObject.subviews) {
        if([subView isKindOfClass: [UITextField class]]){
            UITextField *searchField = (UITextField *)subView;
            CGFloat myWidth = 26.0f;
            CGFloat myHeight = 30.0f;
            UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, myWidth, myHeight)];
            [myButton setImage:[UIImage imageNamed:@"return_icon_del"] forState:UIControlStateNormal];
            [myButton setImage:[UIImage imageNamed:@"return_icon_del"] forState:UIControlStateHighlighted];
            [myButton addTarget:self action:@selector(clearsearchbar) forControlEvents:UIControlEventTouchUpInside];
            searchField.rightView = myButton;
            searchField.rightViewMode = UITextFieldViewModeAlways;
            searchField.clearButtonMode = UITextFieldViewModeNever;
        }
    }
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    self.placeHolderLbl.hidden = YES;
}

- (void)clearsearchbar {
    _searchBarView.text = nil;
    self.placeHolderLbl.hidden = NO;
    [_searchBarView resignFirstResponder];
}


- (void)cancelSearch {
    [_searchBarView resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)showMenu:(UITapGestureRecognizer *)tap
{
    UIView *sender = tap.view;
    NSArray *menuItems =
    @[
      
      [KxMenuItem menuItem:@"位置"
                     image:nil
                    target:self
                    action:@selector(pushMenuItem:)],
      
      [KxMenuItem menuItem:@"车队名称"
                     image:[UIImage imageNamed:@"action_icon"]
                    target:self
                    action:@selector(pushMenuItem:)],
      
      [KxMenuItem menuItem:@"车牌"
                     image:nil
                    target:self
                    action:@selector(pushMenuItem:)],
      ];
    KxMenuItem *first = menuItems[0];
//    first.foreColor = [UIColor colorWithRed:47/255.0f green:112/255.0f blue:225/255.0f alpha:1.0];
    first.alignment = NSTextAlignmentCenter;
    
    KxMenuItem *second = menuItems[1];
    //    first.foreColor = [UIColor colorWithRed:47/255.0f green:112/255.0f blue:225/255.0f alpha:1.0];
    second.alignment = NSTextAlignmentCenter;
    KxMenuItem *thrid = menuItems[2];
    //    first.foreColor = [UIColor colorWithRed:47/255.0f green:112/255.0f blue:225/255.0f alpha:1.0];
    thrid.alignment = NSTextAlignmentCenter;
    CGRect rect = CGRectMake(sender.frame.origin.x, _searchBarView.frame.origin.y, sender.frame.size.width, sender.frame.size.height);
    [KxMenu showMenuInView:self.view
                  fromRect:rect
                 menuItems:menuItems];

    
      
};

- (void) pushMenuItem:(id)sender
{
    NSLog(@"%@", sender);
    KxMenuItem *data = sender;
    _selectBtn.titleLab.text = data.title;
    if ([data.title isEqualToString:@"车队名称"]) {
        self.placeHolderLbl.text = @"请输入您要查询的车队名称";
    }else if ([data.title isEqualToString:@"位置"]){
        self.placeHolderLbl.text = @"请输入您要查询的位置";
    }else{
        self.placeHolderLbl.text = @"请输入您要查询的车牌";
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
